<template>
    <a-modal :width="550"
             :title="title"
             destroyOnClose
             :maskClosable="false"
             v-model="visible">
        <a-form-model
                ref="ruleForm"
                small
                :model="form"
                :rules="rules"
                :label-col="labelCol"
                :wrapper-col="wrapperCol">
            <a-form-model-item label="账号名称" prop="userName">
                <a-input placeholder="请输入账号名称" @keypress="keyPress" v-model="form.userName" />
            </a-form-model-item>
            <a-form-model-item label="登录密码" prop="password">
                <a-input type="password"  @keypress="keyPress" placeholder="请输入登录密码" v-model="form.password">
                    <a-icon slot="prefix" type='lock' />
                </a-input>
            </a-form-model-item>
            <a-form-model-item label="确认密码" prop="confimPassword">
                <a-input type="password" @keypress="keyPress" placeholder="请确认密码" v-model="form.confimPassword">
                    <a-icon slot="prefix" type='lock' />
                </a-input>
            </a-form-model-item>
            <a-form-model-item label="登录IP" prop="loginIp">
                <a-input placeholder="请输入登录IP地址" @keypress="keyPress" v-model="form.loginIp" />
            </a-form-model-item>
            <a-form-model-item label="是否禁用" prop="hideFlag">
                <a-radio-group v-model="form.useFlag">
                    <a-radio :value="false">否</a-radio>
                    <a-radio :value="true">是</a-radio>
                </a-radio-group>
            </a-form-model-item>
        </a-form-model>
        <div slot="footer" class="text-c">
            <a-button @click="close">关闭</a-button>
            <a-button type="primary" :loading="loading" @click="sure">确定</a-button>
        </div>
    </a-modal>
</template>

<script>
    export default {
        data () {
            return {
                form: {
                    userName: '',
                    loginIp: '',
                    useFlag: false,
                    password: '',
                    confimPassword: '',
                    id: '',
                },
                labelCol: { span: 4 },
                wrapperCol: { span: 20 },
                rules: {
                    userName: [
                        { required: true, message: '请输入账号名称', trigger: 'blur' },
                    ],
                    password: [
                        { required: true, message: '请输入密码', trigger: 'blur' },
                        { validator:this.validPsw}
                    ],
                    confimPassword: [
                        { required: true, message: '请确认密码', trigger: 'blur' },
                        { validator:this.validPsw}
                    ],
                },
                visible: false,
                title: '',
                actionType: null,
                loading: false,
            };
        },
        watch: {
            visible (val) {
                if (!val) {
                    this.close();
                }
            }
        },
        methods: {
            sure () {
                this.$refs.ruleForm.validate(valid => {
                    if (valid) {
                        this.loading = true;
                        let tipMsg = this.actionType === 0 ? '新增成功' : '编辑成功';
                        let api = this.actionType === 0 ? this.$api.permission.addadminuser : this.$api.permission.editadminuser;
                        api(this.form).then(res => {
                            this.loading = false;
                            if (res && res.code !== 200) {
                                this.$message.error(res.errorMsg);
                                return;
                            }
                            this.$message.success(tipMsg);
                            this.$emit('done');
                            this.close();
                        }).catch(err => {
                            this.loading = false;
                            this.$message.error(err.errorMsg);
                        });
                    }
                });
            },
            show (data,title,actionType) {
                let temData = JSON.parse(JSON.stringify(data));
                this.title = title;
                this.actionType = actionType;
                if (actionType === 1) {
                    this.form = {
                        userName: temData.userName,
                        loginIp: temData.loginIp,
                        useFlag: temData.useFlag,
                        password: '',
                        confimPassword: '',
                        id: temData.id,
                    };
                }
                this.visible = true;
            },
            close () {
                this.form = {
                    userName: '',
                    loginIp: '',
                    useFlag: false,
                    password: '',
                    confimPassword: '',
                    id: '',
                };
                this.title = '';
                this.actionType = null;
                this.loading = false;
                this.visible = false;
            },
            //校验密码
            validPsw (rule, value, callback) {
                let patrn = /[\u4e00-\u9fa5]/i;
                if(value){
                    if(patrn.test(value)){
                        callback('只能输入6-16位非汉字密码');
                    }else if(value.length<6 || value.length>16) {
                        callback('只能输入6到16位数');
                    }else if(rule.field === "confimPassword"){
                        if (value && value !== this.form.password) {
                            callback('两次密码输入不一致');
                        }else{
                            callback();
                        }
                    }else{
                        callback();
                    }
                }else{
                    callback();
                }
            },
        }
    };
</script>

<style scoped lang="scss">

</style>
